﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Web.UI;
using UTSDAL.LINQSQL.Model;
using UTSDAL.LINQSQL.DAO;


namespace UTS.Controls
{
    [
        DefaultProperty("Text"),
        ToolboxData("<{0}:ModuleEventDropDownList runat=\"server\"> </{0}:ModuleEventDropDownList>")
    ]
    public class ModuleEventDropDownList : System.Web.UI.WebControls.DropDownList
    {


        private ModuleEventDAO dao = new ModuleEventDAO();

        private void init()
        {

            this.DataSource = dao.selectAll();
            this.DataBind();
            DropDownUtils.addDefaultLabel(this, "Select a Module Event");
            this.DataTextField = "ModuleEventLabel";
            this.DataValueField = "ModuleEventId";
            
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                init();
            }
            
        }

        public void reload()
        {
            init();
        }


        private ModuleEvent moduleEvent;

        public ModuleEvent ModuleEvent
        {
            get { 
                if(StringUtils.isValidGuid(this.SelectedValue.ToString()))
                {
                    moduleEvent = dao.findById(new Guid(this.SelectedValue.ToString()));
                }
                else
                {
                    moduleEvent = null;
                }
                return moduleEvent; 
            }
            set { 
                
                moduleEvent = value; 
                if(moduleEvent!=null)
                {
                    this.SelectedValue = moduleEvent.ModuleEventId.ToString();
                }
            }
        }

    }
}